﻿
using EasyStep.Attributes;
using OpenQA.Selenium;
using EasyStep.TestExecution.Data;
using EasyStep.TestExecution.Interfaces;
using System;
using System.Linq;
namespace EasyStep.TestActions.Actions.ExecuteTest
{
  

    [TestAction(typeof(ExecuteBase), "Test", RequireLocator = true, RequireData = false,RequireScreenShot=false)]
    public class ExecuteTest : ITestAction<IWebDriver>
    {
        public void Execute(IWebDriver browser, TestStepResult data, IExecuteDataContext context)
        {
            data.ExecutionLog.AppendLine("Try to find test by name:" + data.Locator);
            var testInfo = context.TestInfoManager.GetTest(context.CurrentProject, data.Locator, true);
            data.ExecutionLog.AppendFormat("test been found?{0},\r\n contains:{1} test steps\r\n", testInfo != null, testInfo == null || testInfo.Steps == null ? -1 : testInfo.Steps.Count());
            if (testInfo == null)
            {
                data.Result = TestExecution.ExecutionResult.Failed;
                throw new NullReferenceException(string.Format("Can't Execute child Test (with name:[{0}]) for current project:[{1}]!", data.Locator, context.CurrentProject));
            }
            data.Children = testInfo.Steps.Select(s => new TestStepResult(s));
        }
    }
    

    [TestAction(null, "Execute", IsAbstract = true)]
    public class ExecuteBase : ITestAction<IWebDriver>
    {
       
        public void Execute(IWebDriver browser, TestStepResult data, IExecuteDataContext context)
        {
            throw new NotImplementedException();
        }
    }
}
